package cn.passlink.bletoothchat.Adapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

import cn.passlink.bletoothchat.Entity.Friends;
import cn.passlink.bletoothchat.Entity.Groups;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.mActivity.FriendTalkingActivity;
import cn.passlink.bletoothchat.mActivity.RelationshipActivity;

public class FriendsAdapter extends BaseAdapter {
    private Context context;
    private List<Friends> friendsList;        // 声明一个标志来跟踪当前的状态
    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;


    public FriendsAdapter(Context context, List<Friends> friendsList) {
        this.context = context;
        this.friendsList = friendsList;
        // 初始化数据库辅助类
        databaseHelper = new MyDatabaseHelper(context,2);
        // 获取可写入的数据库实例
        database = databaseHelper.getWritableDatabase();
    }

    @Override
    public int getCount() {
        return friendsList.size();
    }

    @Override
    public Object getItem(int position) {
        return friendsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.friends_item, parent, false);

            holder = new ViewHolder();
            holder.usernameTextView = convertView.findViewById(R.id.friend_name_TextView);
            holder.phoneTextView = convertView.findViewById(R.id.friend_phone_TextView);
            holder.academyTextView = convertView.findViewById(R.id.friend_academyTextView);
            holder.classesTextView = convertView.findViewById(R.id.friend_classesTextView);
            holder.gradeTextView = convertView.findViewById(R.id.friend_grade_TextView);
            holder.showFriendsDetails=convertView.findViewById(R.id.show_friends_details);
            holder.Del=convertView.findViewById(R.id.friends_item_del);
            holder.Connect=convertView.findViewById(R.id.friends_item_connect);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Friends friends = friendsList.get(position);
        holder.usernameTextView.setText(friends.getUsername());
        holder.phoneTextView.setText(friends.getPhone());
        holder.academyTextView.setText(friends.getAcademy());
        holder.classesTextView.setText(friends.getClasses());
        holder.gradeTextView.setText(friends.getGrade());

        holder.Del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理Del按钮的点击事件
                // 编写您想要执行的代码逻辑
                // 处理Del按钮的点击事件
                Friends mFriends = friendsList.get(position);
                String phone = mFriends.getPhone(); // 获取电话号码
                String whereClause = "phone = ?";
                String[] whereArgs = {phone};
                int rowsAffected = database.delete("friends", whereClause, whereArgs);
                Log.d("Delete", "Rows deleted: " + rowsAffected);

// 更新数据源列表
                // 重新设置适配器
                if (rowsAffected > 0) {
                    friendsList.remove(position);
                    notifyDataSetChanged();
                    Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    // 更新数据源列表


                }
            }
        });

        holder.Connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Friends mFriends=friendsList.get(position);;
                // 处理Connect按钮的点击事件
                // 编写您想要执行的代码逻辑
                goToTargetActivity(mFriends.getUsername(),mFriends.getPhone());

            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder holder = (ViewHolder) v.getTag();

                if (holder.showFriendsDetails.getVisibility()!=View.GONE) {
                    // 隐藏第二个 LinearLayout
                    holder.showFriendsDetails.setVisibility(View.GONE);

                } else {
                    // 显示第二个 LinearLayout
                    holder.showFriendsDetails.setVisibility(View.VISIBLE);
                }

            }
        });

        return convertView;
    }

    private static class ViewHolder {
        TextView usernameTextView;
        TextView phoneTextView;
        TextView gradeTextView;
        TextView academyTextView;
        TextView classesTextView;

        LinearLayout showFriendsDetails;

        ImageView Del;
        ImageView Connect;
    }

    public void goToTargetActivity(String whoName,String phone) {

//      // 创建 Intent
        Intent intent = new Intent(context, FriendTalkingActivity.class);
                // 传递数据（可选）
                intent.putExtra("whoName", whoName);
                intent.putExtra("whoPhone", phone);
                // 启动 Activity
                context.startActivity(intent);
    }
}
